package com.plian.system.wrapper.util;

import com.plian.Tools.EncryptUtil;
import com.plian.Tools.StringPool;
import com.plian.Tools.StringUtil;
import com.plian.spring.SpringUtil;
import com.plian.system.constant.sys.BaseConstant;
import com.plian.system.entity.pm.realproperty.RealProperty;
import com.plian.system.enumeratea.CarInfoStatus;
import com.plian.system.service.pm.realproperty.IRealPropertyService;
import com.plian.system.service.pm.realproperty.impl.RealPropertyServiceImpl;
import org.mapstruct.Named;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.plian.system.wrapper.util.WrapperConstant.*;

/**
 * @description:
 * @author: gangan
 * @create: 2022-01-10 20:42
 */
@Component
public class WrapperUtil {

    /**
     * String to list（,分割）
     * @param string
     * @return
     */
    @Named(STRING_TO_LIST)
    public List<String> stringToList(String string){
        if (StringUtil.isBlank(string)){
            return null;
        }
        return Stream.of(string.split(StringPool.COMMA)).collect(Collectors.toList());
    }

    /**
     * systemStatus
     * @param systemStatus
     * @return
     */
    @Named(SYSTEM_STATUS)
    public String systemStatus(Integer systemStatus){
        return systemStatus != null && systemStatus == 1 ? "正常" : "失效";
    }

    /**
     * null 转empty
     * @param str
     * @return
     */
    @Named(STR_NULL_TO_EMPTY)
    public String strNullToEmpty(String str){
        return str == null  ? "" : str;
    }

    @Named(REAL_PROPERTY_ID_TO_NAME)
    public String realPropertyIdToName(Long realPropertyId){
        if (realPropertyId != null && realPropertyId != 0){
            IRealPropertyService realPropertyService = SpringUtil.getBean(RealPropertyServiceImpl.class);
            RealProperty realProperty = realPropertyService.findLatestByInitialId(realPropertyId);
            if (realProperty != null){
                if (realProperty.getRealPropertyNature() != null && realProperty.getRealPropertyNature() == 2){
                    return realProperty.getHouseName();
                }
                return realProperty.getLandName();
            }
        }
        return null;
    }

    /**
     * 1 转否 2转是
     * @param isOrNo
     * @return
     */
    @Named(IS_OR_NO)
    public String isOrNo(Integer isOrNo){
        if (isOrNo == null){
            return null;
        }
        if (isOrNo == 1){
            return "否";
        }

        if (isOrNo == 2){
            return "是";
        }
        return null;
    }

    /**
     * 判断 填报树是否有下级企业
     * @param tableTypeCode
     * @return
     */
    @Named(BULLETIN_CHECK_HAS_CHILD)
    public Boolean bulletinCheckHasChild(String tableTypeCode){
       if ("6".equals(tableTypeCode)){
           return true;
       }
        return false;
    }

    @Named(STRING_CONCAT)
    public String stringConcat(String a, String b){
        return a + " " + b;
    }

    @Named(REFORM_OUT_TYPE)
    public String reformOutType(Integer outType){
        if (outType == null){
            return null;
        }
        if (outType != null && outType == 1){
            return "产权注销";
        }
        return"混改注销";
    }


    /**
     * 数据加密 base64
     * @param data
     * @return
     */
    @Named(ENCRYPT_BASE64_DATA)
    public String encryptBase64Data(String data){
        return EncryptUtil.encryptBase64(data, BaseConstant.BASE64_KEY);
    }

    /**
     * 数据解密 base64
     * @param data
     * @return
     */
    @Named(DECRYPT_BASE64_DATA)
    public String decryptBase64Data(String data){
        return EncryptUtil.decryptBase64(data, BaseConstant.BASE64_KEY);
    }

    @Named(CAR_STATUS_TO_NAME)
    public String carStatusToName(Integer carStatus){
        if (carStatus == null){
            return null;
        }
        return CarInfoStatus.getStatusNameByStatus(carStatus);
    }
}
